﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Serialization.Formatters.Soap;

namespace MYTech.Data
{
    /// <summary>
    /// Image帮助类
    /// </summary>
    public class ImageHelper
    {
        /// <summary>
        /// 读取Image到byte[]
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static byte[] GetBytesByImage(Image image)
        {
            byte[] bytes = null;
            if (image != null)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    Bitmap bmp = new Bitmap(image);
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    bytes = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(bytes, 0, Convert.ToInt32(ms.Length));
                    bmp.Dispose();
                }
            }
            return bytes;
        }

        /// <summary>
        /// 从指定路径读取Image到byte[]
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static byte[] GetBytesByImagePath(string filePath)
        {
            byte[] bytes = null;
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    bytes = br.ReadBytes((int)fs.Length);
                }
            }
            return bytes;
        }

        /// <summary>
        /// 读取byte[]并转化为图片
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Image GetImageByBytes(byte[] bytes)
        {
            Image image = null;
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                ms.Write(bytes, 0, bytes.Length);
                image = Image.FromStream(ms, true);
            }
            return image;
        }

        /// <summary>
        /// 读取byte[]并转化为指定大小的图片
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="length"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        public static Bitmap GetBitmapByBytes(byte[] bytes, int length, int width)
        {
            Bitmap image = new Bitmap(width, length);
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                image.Save(ms, ImageFormat.Bmp);
                image.Dispose();
            }
            return image;
        }

        /// <summary>
        /// Image图片转化为XML保存
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <param name="imagePath"></param>
        public static void ImageToXml(string xmlPath, string imagePath)
        {
            using (Stream stream = new FileStream(xmlPath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                SoapFormatter f = new SoapFormatter();
                Image img = Image.FromFile(imagePath);
                f.Serialize(stream, img);
                stream.Close();
            }
        }

        /// <summary>
        /// 从xml文件读取Image图片
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static Image XmlToImage(string xmlPath)
        {
            using (Stream stream = new FileStream(xmlPath, FileMode.Open))
            {
                SoapFormatter f = new SoapFormatter();
                Image image = (Image)f.Deserialize(stream);
                stream.Close();
                return image;
            }
        }

        /// <summary>
        /// 保存图片到物理路径
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="image"></param>
        /// <param name="format"></param>
        public static void Save(string filePath, Image image, ImageFormat format)
        {
            image.Save(filePath, format);
            image.Dispose();
        }

        /// <summary>
        /// 保存图片到物理路径
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="image"></param>
        public static void Save(string filePath, Image image)
        {
            Save(filePath, image, ImageFormat.Jpeg);
        }

        /// <summary>
        /// 加载图片
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static Image Load(string filePath)
        {
            using (Image img = Image.FromFile(filePath))
            {
                return img;
            }
        }
    }
}
